#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <assert.h>
#include "tcpa_monitor.h"
#include "read_json.h"
#include "user_proc.h"
#include "tcpa_util.h"

void test_tcpa_monitor_create() {
    tcpa_monitor_t *monitor = tcpa_monitor_create();
    assert(monitor != NULL);
    assert(strcmp(monitor->tcpam_cfg_file_ctx.tcpa_cfg_file[TCPA_CFG].cfg_name, "tcpa_cfg") == 0);
    assert(strcmp(monitor->tcpam_cfg_file_ctx.tcpa_cfg_file[TCPA_CFG].file_path, TCPA_CFG_FILE_DEFAULT_PATH) == 0);
    assert(strcmp(monitor->tcpam_cfg_file_ctx.tcpa_cfg_file[WHITELIST_CFG].cfg_name, "whitelist") == 0);
    assert(strcmp(monitor->tcpam_cfg_file_ctx.tcpa_cfg_file[WHITELIST_CFG].file_path, TCPA_WHITELIST_CFG_FILE_DEFAULT_PATH) == 0);
    tcpa_monitor_destroy(monitor);
}

void test_tcpa_monitor_stop() {
    tcpa_monitor_t *monitor = tcpa_monitor_create();
    assert(monitor != NULL);
    int init_ret = tcpa_monitor_init(monitor);
    assert(init_ret == 0);
    int ret = tcpa_monitor_stop(monitor);
    assert(ret == 0);
    assert(monitor->monitor_status == TCPA_MONITOR_STATUS_STOP);
    tcpa_monitor_destroy(monitor);
}

void test_create_default_tcpa_cfg_file() {
    char file_path[] = "test_tcpa_cfg.json";
    create_default_tcpa_cfg_file(file_path);
    FILE *fp = fopen(file_path, "r");
    assert(fp != NULL);
    fclose(fp);
    remove(file_path);
}

void test_tcpa_cfg_init_from_file() {
    tcpa_monitor_t *monitor = tcpa_monitor_create();
    assert(monitor != NULL);
    int init_ret = tcpa_monitor_init(monitor);
    assert(init_ret == 0);
    int ret = tcpa_cfg_update_from_file(monitor);
    assert(ret == 0);
    tcpa_monitor_destroy(monitor);
}

void test_creat_default_whitelist_file() {
    char file_path[] = "test_whitelist.json";
    creat_default_whitelist_file(file_path);
    FILE *fp = fopen(file_path, "r");
    assert(fp != NULL);
    fclose(fp);
    remove(file_path);
}

void test_whitelist_cfg_init_from_file() {
    tcpa_monitor_t *monitor = tcpa_monitor_create();
    assert(monitor != NULL);
    int init_ret = tcpa_monitor_init(monitor);
    assert(init_ret == 0);
    int ret = whitelist_cfg_init_from_file(monitor);
    assert(ret == 0);
    tcpa_monitor_destroy(monitor);
}

void test_listen_cfg_file_modify() {
    tcpa_monitor_t *monitor = tcpa_monitor_create();
    assert(monitor != NULL);
    int init_ret = tcpa_monitor_init(monitor);
    assert(init_ret == 0);
    int ret = listen_cfg_file_modify(monitor);
    assert(ret == 0);
    tcpa_monitor_stop(monitor);
    tcpa_monitor_destroy(monitor);
}

void test_listen_rcst_login() {
    tcpa_monitor_t *monitor = tcpa_monitor_create();
    assert(monitor != NULL);
    int init_ret = tcpa_monitor_init(monitor);
    assert(init_ret == 0);
    int ret = listen_rcst_login(monitor);
    assert(ret == 0);
    tcpa_monitor_destroy(monitor);
}

void test_tcpa_monitor_init() {
    tcpa_monitor_t *monitor = tcpa_monitor_create();
    assert(monitor != NULL);
    int ret = tcpa_monitor_init(monitor);
    assert(ret == 0);
    tcpa_monitor_stop(monitor);
    tcpa_monitor_destroy(monitor);
}

int main() {
    printf("Start testing...\n");

    printf("---Testing tcpa_monitor_create()...\n");
    test_tcpa_monitor_create();
    printf("---Testing tcpa_monitor_stop()...\n");
    test_tcpa_monitor_stop();
    printf("---Testing create_default_tcpa_cfg_file()...\n");
    test_create_default_tcpa_cfg_file();
    printf("---Testing tcpa_cfg_update_from_file()...\n");
    test_tcpa_cfg_init_from_file();
    printf("---Testing creat_default_whitelist_file()...\n");
    test_creat_default_whitelist_file();
    printf("---Testing whitelist_cfg_init_from_file()...\n");
    test_whitelist_cfg_init_from_file();
    printf("---Testing listen_cfg_file_modify()...\n");
    test_listen_cfg_file_modify();
    printf("---Testing listen_rcst_login()...\n");
    test_listen_rcst_login();
    printf("---Testing tcpa_monitor_init()...\n");
    test_tcpa_monitor_init();
    printf("All tests passed!\n");
    return 0;
}